<% context = local_assigns[:context] || :order %>

<% if context == :order %>
  <p>
    <%= condition.class.human_attribute_name(:description) %>
  </p>
  <div class="field promo-condition-option-values">
    <div class="param-prefix hidden" data-param-prefix="<%= param_prefix %>"></div>
    <div class="row">
      <div class="col-5"><%= label_tag nil, Spree::Product.model_name.human %></div>
      <div class="col-6"><%= label_tag nil, plural_resource_name(Spree::OptionValue) %></div>
      <div class="col-1">&nbsp;</div>
    </div>

    <div class="form-group">
      <div data-controller="product-option-values">
        <template data-product-option-values-target="template">
          <%= render "solidus_promotions/admin/condition_fields/line_item_option_value/option_value_fields", product_option_values: [nil, []], form:, index: 0 %>
        </template>
        <% form.object.preferred_eligible_values.each.with_index do |product_option_values, index| %>
          <%= render "solidus_promotions/admin/condition_fields/line_item_option_value/option_value_fields", product_option_values:, form:, index: %>
        <% end %>
        <div class="mt-3" data-product-option-values-target="links">
          <%= link_to t(:add_product, scope: [:solidus_promotions, :option_value_condition]), "#", class: "btn btn-outline-primary", data: { action: "click->product-option-values#add_row" } %>
        </div>
      </div>
    </div>
  </div>
<% else %>
  <p>
    <%= SolidusPromotions::Conditions::LineItemOptionValue.human_attribute_name(:description) %>
  </p>
  <table>
    <thead>
      <tr>
        <th>
          <%= Spree::Product.model_name.human %>
        </th>
        <th>
          <%= Spree::OptionValue.model_name.human(count: :other) %>
        </th>
      </tr>
    </thead>
    <tbody>
      <% condition.preferred_eligible_values.each do |product_id, option_value_ids| %>
        <tr>
          <td>
            <%= Spree::Product.find(product_id).name %>
          </td>
          <td>
            <%= Spree::OptionValue.where(id: option_value_ids).map(&:name).join(", ") %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% end %>
